package com.co.user.controller;

import com.co.common.entity.Result;
import com.co.user.pojo.History;
import com.co.user.service.HistoryService;
import com.co.video.pojo.Detail;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ldl.plus
 * @date 2020年06月22日  0:41
 */

@RestController
@RequestMapping("/user/history")

public class HistoryController {

    @Autowired
    private HistoryService historyService;

    /**
     * UsHistory分页条件搜索实现
     *
     * @param history
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<History>> findPage(@RequestBody(required = false) History history, @PathVariable int page, @PathVariable int size) {
        //调用UsHistoryService实现分页条件查询UsHistory
        PageInfo<History> pageInfo = historyService.findPage(history, page, size);
        return Result.ok(pageInfo);
    }

    /**
     * UsHistory分页搜索实现
     *
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<History>> findPage(@PathVariable int page, @PathVariable int size) {
        //调用UsHistoryService实现分页查询UsHistory
        PageInfo<History> pageInfo = historyService.findPage(page, size);
        return Result.ok(pageInfo);
    }

    /**
     * 多条件搜索品牌数据
     *
     * @param history
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<History>> findList(@RequestBody(required = false) History history) {
        //调用UsHistoryService实现条件查询UsHistory
        List<History> list = historyService.findList(history);
        return Result.ok(list);
    }

    /**
     * 根据ID删除品牌数据
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result<?> delete(@PathVariable String id) {
        //调用UsHistoryService实现根据主键删除
        historyService.delete(id);
        return Result.ok();
    }

    /**
     * 修改UsHistory数据
     *
     * @param history
     * @param id
     * @return
     */
    @PutMapping(value = "/{id}")
    public Result<?> update(@RequestBody History history, @PathVariable String id) {
        //设置主键值
        history.setId(id);
        //调用UsHistoryService实现修改UsHistory
        historyService.update(history);
        return Result.ok();
    }

    /**
     * 新增UsHistory数据
     *
     * @param history
     * @return
     */
    @PostMapping
    public Result<?> add(@RequestBody History history) {
        //调用UsHistoryService实现添加UsHistory
        historyService.add(history);
        return Result.ok();
    }

    /**
     * 删除所有历史记录
     *
     * @return
     */
    @DeleteMapping("/deleteAll")
    public Result deleteAll() {
        historyService.deleteAll();
        return Result.ok("历史记录全部删除");
    }

    /**
     * 根据ID查询UsHistory数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<History> findById(@PathVariable String id) {
        //调用UsHistoryService实现根据主键查询UsHistory
        History history = historyService.findById(id);
        return Result.ok(history);
    }

    /**
     * 查询UsHistory全部数据
     *
     * @return
     */
    @GetMapping
    public Result<List<History>> findAll() {
        //调用UsHistoryService实现查询所有UsHistory
        List<History> list = historyService.findAll();
        return Result.ok(list);
    }

    /**
     * 根据用户名查询用户浏览历史
     *
     * @param username
     * @return
     */
    @GetMapping("/user/{username}")
    public Result<Object> findHistoryByUsername(@PathVariable("username") String username) {
        List<Detail> historyByUsername = historyService.findHistoryByUsername(username);
        return Result.ok(historyByUsername);
    }


}

